home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / share / pygtk / 2.0 / codegen / docgen.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2006-01-20  |  28KB  |  829 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import sys
  5. import os
  6. import string
  7. import re
  8. import getopt
  9. import defsparser
  10. import definitions
  11. import override
  12. import docextract
  13.  
  14. class Node:
  15.     
  16.     def __init__(self, name, interfaces = []):
  17.         self.name = name
  18.         self.interfaces = interfaces
  19.         self.subclasses = []
  20.  
  21.     
  22.     def add_child(self, node):
  23.         self.subclasses.append(node)
  24.  
  25.  
  26.  
  27. def build_object_tree(parser):
  28.     objects = parser.objects[:]
  29.     pos = 0
  30.     while pos < len(objects):
  31.         parent = objects[pos].parent
  32.         for i in range(pos + 1, len(objects)):
  33.             if objects[i].c_name == parent:
  34.                 objects.insert(i + 1, objects[pos])
  35.                 del objects[pos]
  36.                 break
  37.                 continue
  38.         else:
  39.             pos = pos + 1
  40.     root = Node(None)
  41.     nodes = {
  42.         None: root }
  43.     for obj_def in objects:
  44.         parent_node = nodes[obj_def.parent]
  45.         node = Node(obj_def.c_name, obj_def.implements)
  46.         parent_node.add_child(node)
  47.         nodes[node.name] = node
  48.     
  49.     if parser.interfaces:
  50.         interfaces = Node('gobject.GInterface')
  51.         root.add_child(interfaces)
  52.         nodes[interfaces.name] = interfaces
  53.         for obj_def in parser.interfaces:
  54.             node = Node(obj_def.c_name)
  55.             interfaces.add_child(node)
  56.             nodes[node.name] = node
  57.         
  58.     
  59.     if parser.boxes:
  60.         boxed = Node('gobject.GBoxed')
  61.         root.add_child(boxed)
  62.         nodes[boxed.name] = boxed
  63.         for obj_def in parser.boxes:
  64.             node = Node(obj_def.c_name)
  65.             boxed.add_child(node)
  66.             nodes[node.name] = node
  67.         
  68.     
  69.     if parser.pointers:
  70.         pointers = Node('gobject.GPointer')
  71.         root.add_child(pointers)
  72.         nodes[pointers.name] = pointers
  73.         for obj_def in parser.pointers:
  74.             node = Node(obj_def.c_name)
  75.             pointers.add_child(node)
  76.             nodes[node.name] = node
  77.         
  78.     
  79.     return root
  80.  
  81.  
  82. class DocWriter:
  83.     
  84.     def __init__(self):
  85.         self.parser = defsparser.DefsParser(())
  86.         self.overrides = override.Overrides()
  87.         self.classmap = { }
  88.         self.docs = { }
  89.  
  90.     
  91.     def add_sourcedirs(self, source_dirs):
  92.         self.docs = docextract.extract(source_dirs, self.docs)
  93.  
  94.     
  95.     def add_tmpldirs(self, tmpl_dirs):
  96.         self.docs = docextract.extract_tmpl(tmpl_dirs, self.docs)
  97.  
  98.     
  99.     def add_docs(self, defs_file, overrides_file, module_name):
  100.         '''parse information about a given defs file'''
  101.         self.parser.filename = defs_file
  102.         self.parser.startParsing(defs_file)
  103.         if overrides_file:
  104.             self.overrides.handle_file(overrides_file)
  105.         
  106.         for obj in self.parser.objects:
  107.             if not self.classmap.has_key(obj.c_name):
  108.                 self.classmap[obj.c_name] = '%s.%s' % (module_name, obj.name)
  109.                 continue
  110.         
  111.         for obj in self.parser.interfaces:
  112.             if not self.classmap.has_key(obj.c_name):
  113.                 self.classmap[obj.c_name] = '%s.%s' % (module_name, obj.name)
  114.                 continue
  115.         
  116.         for obj in self.parser.boxes:
  117.             if not self.classmap.has_key(obj.c_name):
  118.                 self.classmap[obj.c_name] = '%s.%s' % (module_name, obj.name)
  119.                 continue
  120.         
  121.         for obj in self.parser.pointers:
  122.             if not self.classmap.has_key(obj.c_name):
  123.                 self.classmap[obj.c_name] = '%s.%s' % (module_name, obj.name)
  124.                 continue
  125.         
  126.  
  127.     
  128.     def pyname(self, name):
  129.         return self.classmap.get(name, name)
  130.  
  131.     
  132.     def __compare(self, obja, objb):
  133.         return cmp(self.pyname(obja.c_name), self.pyname(objb.c_name))
  134.  
  135.     
  136.     def output_docs(self, output_prefix):
  137.         files = []
  138.         hierarchy = build_object_tree(self.parser)
  139.         filename = self.create_filename('hierarchy', output_prefix)
  140.         fp = open(filename, 'w')
  141.         self.write_full_hierarchy(hierarchy, fp)
  142.         fp.close()
  143.         obj_defs = self.parser.objects + self.parser.interfaces + self.parser.boxes + self.parser.pointers
  144.         obj_defs.sort(self._DocWriter__compare)
  145.         for obj_def in obj_defs:
  146.             filename = self.create_filename(obj_def.c_name, output_prefix)
  147.             fp = open(filename, 'w')
  148.             if isinstance(obj_def, definitions.ObjectDef):
  149.                 self.output_object_docs(obj_def, fp)
  150.             elif isinstance(obj_def, definitions.InterfaceDef):
  151.                 self.output_interface_docs(obj_def, fp)
  152.             elif isinstance(obj_def, definitions.BoxedDef):
  153.                 self.output_boxed_docs(obj_def, fp)
  154.             elif isinstance(obj_def, definitions.PointerDef):
  155.                 self.output_boxed_docs(obj_def, fp)
  156.             
  157.             fp.close()
  158.             files.append((os.path.basename(filename), obj_def))
  159.         
  160.         if files:
  161.             filename = self.create_toc_filename(output_prefix)
  162.             fp = open(filename, 'w')
  163.             self.output_toc(files, fp)
  164.             fp.close()
  165.         
  166.  
  167.     
  168.     def output_object_docs(self, obj_def, fp = sys.stdout):
  169.         self.write_class_header(obj_def.c_name, fp)
  170.         self.write_heading('Synopsis', fp)
  171.         self.write_synopsis(obj_def, fp)
  172.         self.close_section(fp)
  173.         ancestry = [
  174.             (obj_def.c_name, obj_def.implements)]
  175.         
  176.         try:
  177.             parent = obj_def.parent
  178.             while parent != None:
  179.                 if parent == 'GObject':
  180.                     ancestry.append(('GObject', []))
  181.                     parent = None
  182.                     continue
  183.                 parent_def = self.parser.find_object(parent)
  184.                 ancestry.append((parent_def.c_name, parent_def.implements))
  185.                 parent = parent_def.parent
  186.         except ValueError:
  187.             pass
  188.  
  189.         ancestry.reverse()
  190.         self.write_heading('Ancestry', fp)
  191.         self.write_hierarchy(obj_def.c_name, ancestry, fp)
  192.         self.close_section(fp)
  193.         constructor = self.parser.find_constructor(obj_def, self.overrides)
  194.         if constructor:
  195.             self.write_heading('Constructor', fp)
  196.             self.write_constructor(constructor, self.docs.get(constructor.c_name, None), fp)
  197.             self.close_section(fp)
  198.         
  199.         methods = self.parser.find_methods(obj_def)
  200.         methods = filter((lambda meth, self = self: not self.overrides.is_ignored(meth.c_name)), methods)
  201.         if methods:
  202.             self.write_heading('Methods', fp)
  203.             for method in methods:
  204.                 self.write_method(method, self.docs.get(method.c_name, None), fp)
  205.             
  206.             self.close_section(fp)
  207.         
  208.         self.write_class_footer(obj_def.c_name, fp)
  209.  
  210.     
  211.     def output_interface_docs(self, int_def, fp = sys.stdout):
  212.         self.write_class_header(int_def.c_name, fp)
  213.         self.write_heading('Synopsis', fp)
  214.         self.write_synopsis(int_def, fp)
  215.         self.close_section(fp)
  216.         methods = self.parser.find_methods(int_def)
  217.         methods = filter((lambda meth, self = self: not self.overrides.is_ignored(meth.c_name)), methods)
  218.         if methods:
  219.             self.write_heading('Methods', fp)
  220.             for method in methods:
  221.                 self.write_method(method, self.docs.get(method.c_name, None), fp)
  222.             
  223.             self.close_section(fp)
  224.         
  225.         self.write_class_footer(int_def.c_name, fp)
  226.  
  227.     
  228.     def output_boxed_docs(self, box_def, fp = sys.stdout):
  229.         self.write_class_header(box_def.c_name, fp)
  230.         self.write_heading('Synopsis', fp)
  231.         self.write_synopsis(box_def, fp)
  232.         self.close_section(fp)
  233.         constructor = self.parser.find_constructor(box_def, self.overrides)
  234.         if constructor:
  235.             self.write_heading('Constructor', fp)
  236.             self.write_constructor(constructor, self.docs.get(constructor.c_name, None), fp)
  237.             self.close_section(fp)
  238.         
  239.         methods = self.parser.find_methods(box_def)
  240.         methods = filter((lambda meth, self = self: not self.overrides.is_ignored(meth.c_name)), methods)
  241.         if methods:
  242.             self.write_heading('Methods', fp)
  243.             for method in methods:
  244.                 self.write_method(method, self.docs.get(method.c_name, None), fp)
  245.             
  246.             self.close_section(fp)
  247.         
  248.         self.write_class_footer(box_def.c_name, fp)
  249.  
  250.     
  251.     def output_toc(self, files, fp = sys.stdout):
  252.         fp.write('TOC\n\n')
  253.         for filename, obj_def in files:
  254.             fp.write(obj_def.c_name + ' - ' + filename + '\n')
  255.         
  256.  
  257.     
  258.     def create_filename(self, obj_name, output_prefix):
  259.         '''Create output filename for this particular object'''
  260.         return output_prefix + '-' + string.lower(obj_name) + '.txt'
  261.  
  262.     
  263.     def create_toc_filename(self, output_prefix):
  264.         return self.create_filename(self, 'docs', output_prefix)
  265.  
  266.     
  267.     def write_full_hierarchy(self, hierarchy, fp):
  268.         
  269.         def handle_node(node, fp, indent = ''):
  270.             for child in node.subclasses:
  271.                 fp.write(indent + node.name)
  272.                 if node.interfaces:
  273.                     fp.write(' (implements ')
  274.                     fp.write(string.join(node.interfaces, ', '))
  275.                     fp.write(')\n')
  276.                 else:
  277.                     fp.write('\n')
  278.                 handle_node(child, fp, indent + '  ')
  279.             
  280.  
  281.         handle_node(hierarchy, fp)
  282.  
  283.     
  284.     def create_constructor_prototype(self, func_def):
  285.         return func_def.is_constructor_of + '(' + string.join(map((lambda x: x[1]), func_def.params), ', ') + ')'
  286.  
  287.     
  288.     def create_function_prototype(self, func_def):
  289.         return func_def.name + '(' + string.join(map((lambda x: x[1]), func_def.params), ', ') + ')'
  290.  
  291.     
  292.     def create_method_prototype(self, meth_def):
  293.         return meth_def.of_object + '.' + meth_def.name + '(' + string.join(map((lambda x: x[1]), meth_def.params), ', ') + ')'
  294.  
  295.     
  296.     def write_class_header(self, obj_name, fp):
  297.         fp.write('Class %s\n' % obj_name)
  298.         fp.write('======%s\n\n' % '=' * len(obj_name))
  299.  
  300.     
  301.     def write_class_footer(self, obj_name, fp):
  302.         pass
  303.  
  304.     
  305.     def write_heading(self, text, fp):
  306.         fp.write('\n' + text + '\n' + '-' * len(text) + '\n')
  307.  
  308.     
  309.     def close_section(self, fp):
  310.         pass
  311.  
  312.     
  313.     def write_synopsis(self, obj_def, fp):
  314.         fp.write('class %s' % obj_def.c_name)
  315.         if isinstance(obj_def, definitions.ObjectDef):
  316.             bases = []
  317.             if obj_def.parent:
  318.                 bases.append(obj_def.parent)
  319.             
  320.             bases = bases = obj_def.implements
  321.             if bases:
  322.                 fp.write('(%s)' % string.join(bases, ', '))
  323.             
  324.         
  325.         fp.write(':\n')
  326.         constructor = self.parser.find_constructor(obj_def, self.overrides)
  327.         if constructor:
  328.             prototype = self.create_constructor_prototype(constructor)
  329.             fp.write('    def %s\n' % prototype)
  330.         
  331.         methods = self.parser.find_methods(obj_def)
  332.         methods = filter((lambda meth, self = self: not self.overrides.is_ignored(meth.c_name)), methods)
  333.         for meth in methods:
  334.             prototype = self.create_method_prototype(meth)
  335.             fp.write('    def %s\n' % prototype)
  336.         
  337.  
  338.     
  339.     def write_hierarchy(self, obj_name, ancestry, fp):
  340.         indent = ''
  341.         for name, interfaces in ancestry:
  342.             fp.write(indent + '+-- ' + name)
  343.             if interfaces:
  344.                 fp.write(' (implements ')
  345.                 fp.write(string.join(interfaces, ', '))
  346.                 fp.write(')\n')
  347.             else:
  348.                 fp.write('\n')
  349.             indent = indent + '  '
  350.         
  351.         fp.write('\n')
  352.  
  353.     
  354.     def write_constructor(self, func_def, func_doc, fp):
  355.         prototype = self.create_constructor_prototype(func_def)
  356.         fp.write(prototype + '\n\n')
  357.         for type, name, dflt, null in func_def.params:
  358.             if func_doc:
  359.                 descr = func_doc.get_param_description(name)
  360.             else:
  361.                 descr = 'a ' + type
  362.             fp.write('  ' + name + ': ' + descr + '\n')
  363.         
  364.         if func_def.ret and func_def.ret != 'none':
  365.             if func_doc and func_doc.ret:
  366.                 descr = func_doc.ret
  367.             else:
  368.                 descr = 'a ' + func_def.ret
  369.             fp.write('  Returns: ' + descr + '\n')
  370.         
  371.         if func_doc and func_doc.description:
  372.             fp.write(func_doc.description)
  373.         
  374.         fp.write('\n\n\n')
  375.  
  376.     
  377.     def write_method(self, meth_def, func_doc, fp):
  378.         prototype = self.create_method_prototype(meth_def)
  379.         fp.write(prototype + '\n\n')
  380.         for type, name, dflt, null in meth_def.params:
  381.             if func_doc:
  382.                 descr = func_doc.get_param_description(name)
  383.             else:
  384.                 descr = 'a ' + type
  385.             fp.write('  ' + name + ': ' + descr + '\n')
  386.         
  387.         if meth_def.ret and meth_def.ret != 'none':
  388.             if func_doc and func_doc.ret:
  389.                 descr = func_doc.ret
  390.             else:
  391.                 descr = 'a ' + meth_def.ret
  392.             fp.write('  Returns: ' + descr + '\n')
  393.         
  394.         if func_doc and func_doc.description:
  395.             fp.write('\n')
  396.             fp.write(func_doc.description)
  397.         
  398.         fp.write('\n\n')
  399.  
  400.  
  401.  
  402. class DocbookDocWriter(DocWriter):
  403.     
  404.     def __init__(self, use_xml = 0):
  405.         DocWriter.__init__(self)
  406.         self.use_xml = use_xml
  407.  
  408.     
  409.     def create_filename(self, obj_name, output_prefix):
  410.         '''Create output filename for this particular object'''
  411.         stem = output_prefix + '-' + string.lower(obj_name)
  412.         if self.use_xml:
  413.             return stem + '.xml'
  414.         else:
  415.             return stem + '.sgml'
  416.  
  417.     
  418.     def create_toc_filename(self, output_prefix):
  419.         if self.use_xml:
  420.             return self.create_filename('classes', output_prefix)
  421.         else:
  422.             return self.create_filename('docs', output_prefix)
  423.  
  424.     __transtable = [
  425.         '-'] * 256
  426.     for digit in '0123456789':
  427.         __transtable[ord(digit)] = digit
  428.     
  429.     for letter in 'abcdefghijklmnopqrstuvwxyz':
  430.         __transtable[ord(letter)] = letter
  431.         __transtable[ord(string.upper(letter))] = letter
  432.     
  433.     __transtable = string.join(__transtable, '')
  434.     
  435.     def make_class_ref(self, obj_name):
  436.         return 'class-' + string.translate(obj_name, self._DocbookDocWriter__transtable)
  437.  
  438.     
  439.     def make_method_ref(self, meth_def):
  440.         return 'method-' + string.translate(meth_def.of_object, self._DocbookDocWriter__transtable) + '--' + string.translate(meth_def.name, self._DocbookDocWriter__transtable)
  441.  
  442.     __function_pat = re.compile('(\\w+)\\s*\\(\\)')
  443.     
  444.     def __format_function(self, match):
  445.         info = self.parser.c_name.get(match.group(1), None)
  446.         if info:
  447.             if isinstance(info, defsparser.FunctionDef):
  448.                 if info.is_constructor_of is not None:
  449.                     return '<function>%s()</function>' % self.pyname(info.is_constructor_of)
  450.                 else:
  451.                     return '<function>' + info.name + '()</function>'
  452.             
  453.             if isinstance(info, defsparser.MethodDef):
  454.                 return '<link linkend="' + self.make_method_ref(info) + '"><function>' + self.pyname(info.of_object) + '.' + info.name + '()</function></link>'
  455.             
  456.         
  457.         return '<function>' + match.group(1) + '()</function>'
  458.  
  459.     __parameter_pat = re.compile('\\@(\\w+)')
  460.     
  461.     def __format_param(self, match):
  462.         return '<parameter>' + match.group(1) + '</parameter>'
  463.  
  464.     __constant_pat = re.compile('\\%(-?\\w+)')
  465.     
  466.     def __format_const(self, match):
  467.         return '<literal>' + match.group(1) + '</literal>'
  468.  
  469.     __symbol_pat = re.compile('#([\\w-]+)')
  470.     
  471.     def __format_symbol(self, match):
  472.         info = self.parser.c_name.get(match.group(1), None)
  473.         if info:
  474.             if isinstance(info, defsparser.FunctionDef):
  475.                 if info.is_constructor_of is not None:
  476.                     return '<methodname>' + self.pyname(info.is_constructor_of) + '</methodname>'
  477.                 else:
  478.                     return '<function>' + info.name + '</function>'
  479.             
  480.             if isinstance(info, defsparser.MethodDef):
  481.                 return '<link linkend="' + self.make_method_ref(info) + '"><methodname>' + self.pyname(info.of_object) + '.' + info.name + '</methodname></link>'
  482.             
  483.             if isinstance(info, defsparser.ObjectDef) and isinstance(info, defsparser.InterfaceDef) and isinstance(info, defsparser.BoxedDef) or isinstance(info, defsparser.PointerDef):
  484.                 return '<link linkend="' + self.make_class_ref(info.c_name) + '"><classname>' + self.pyname(info.c_name) + '</classname></link>'
  485.             
  486.         
  487.         return '<literal>' + match.group(1) + '</literal>'
  488.  
  489.     
  490.     def reformat_text(self, text, singleline = 0):
  491.         text = self._DocbookDocWriter__function_pat.sub(self._DocbookDocWriter__format_function, text)
  492.         text = self._DocbookDocWriter__parameter_pat.sub(self._DocbookDocWriter__format_param, text)
  493.         text = self._DocbookDocWriter__constant_pat.sub(self._DocbookDocWriter__format_const, text)
  494.         text = self._DocbookDocWriter__symbol_pat.sub(self._DocbookDocWriter__format_symbol, text)
  495.         if singleline:
  496.             return text
  497.         
  498.         lines = string.split(string.strip(text), '\n')
  499.         for index in range(len(lines)):
  500.             if string.strip(lines[index]) == '':
  501.                 lines[index] = '</para>\n<para>'
  502.                 continue
  503.                 continue
  504.         
  505.         lines.insert(0, '<para>')
  506.         lines.append('</para>')
  507.         return string.join(lines, '\n')
  508.  
  509.     
  510.     def write_full_hierarchy(self, hierarchy, fp):
  511.         
  512.         def handle_node(node, fp, indent = ''):
  513.             if node.name:
  514.                 fp.write('%s<link linkend="%s">%s</link>' % (indent, self.make_class_ref(node.name), self.pyname(node.name)))
  515.                 if node.interfaces:
  516.                     fp.write(' (implements ')
  517.                     for i in range(len(node.interfaces)):
  518.                         fp.write('<link linkend="%s">%s</link>' % (self.make_class_ref(node.interfaces[i]), self.pyname(node.interfaces[i])))
  519.                         if i != len(node.interfaces) - 1:
  520.                             fp.write(', ')
  521.                             continue
  522.                     
  523.                     fp.write(')\n')
  524.                 else:
  525.                     fp.write('\n')
  526.                 indent = indent + '  '
  527.             
  528.             node.subclasses.sort((lambda a, b: cmp(self.pyname(a.name), self.pyname(b.name))))
  529.             for child in node.subclasses:
  530.                 handle_node(child, fp, indent)
  531.             
  532.  
  533.         if self.use_xml:
  534.             fp.write('<?xml version="1.0" standalone="no"?>\n')
  535.             fp.write('<!DOCTYPE synopsis PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"\n')
  536.             fp.write('    "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">\n')
  537.         
  538.         fp.write('<synopsis>')
  539.         handle_node(hierarchy, fp)
  540.         fp.write('</synopsis>\n')
  541.  
  542.     
  543.     def create_constructor_prototype(self, func_def):
  544.         sgml = [
  545.             '<constructorsynopsis language="python">\n']
  546.         sgml.append('    <methodname>__init__</methodname>\n')
  547.         for type, name, dflt, null in func_def.params:
  548.             sgml.append('    <methodparam><parameter>')
  549.             sgml.append(name)
  550.             sgml.append('</parameter>')
  551.             if dflt:
  552.                 sgml.append('<initializer>')
  553.                 sgml.append(dflt)
  554.                 sgml.append('</initializer>')
  555.             
  556.             sgml.append('</methodparam>\n')
  557.         
  558.         if not func_def.params:
  559.             sgml.append('    <methodparam></methodparam>')
  560.         
  561.         sgml.append('  </constructorsynopsis>')
  562.         return string.join(sgml, '')
  563.  
  564.     
  565.     def create_function_prototype(self, func_def):
  566.         sgml = [
  567.             '<funcsynopsis language="python">\n    <funcprototype>\n']
  568.         sgml.append('      <funcdef><function>')
  569.         sgml.append(func_def.name)
  570.         sgml.append('</function></funcdef>\n')
  571.         for type, name, dflt, null in func_def.params:
  572.             sgml.append('      <paramdef><parameter>')
  573.             sgml.append(name)
  574.             sgml.append('</parameter>')
  575.             if dflt:
  576.                 sgml.append('<initializer>')
  577.                 sgml.append(dflt)
  578.                 sgml.append('</initializer>')
  579.             
  580.             sgml.append('</paramdef>\n')
  581.         
  582.         if not func_def.params:
  583.             sgml.append('      <paramdef></paramdef')
  584.         
  585.         sgml.append('    </funcprototype>\n  </funcsynopsis>')
  586.         return string.join(sgml, '')
  587.  
  588.     
  589.     def create_method_prototype(self, meth_def, addlink = 0):
  590.         sgml = [
  591.             '<methodsynopsis language="python">\n']
  592.         sgml.append('    <methodname>')
  593.         if addlink:
  594.             sgml.append('<link linkend="%s">' % self.make_method_ref(meth_def))
  595.         
  596.         sgml.append(self.pyname(meth_def.name))
  597.         if addlink:
  598.             sgml.append('</link>')
  599.         
  600.         sgml.append('</methodname>\n')
  601.         for type, name, dflt, null in meth_def.params:
  602.             sgml.append('    <methodparam><parameter>')
  603.             sgml.append(name)
  604.             sgml.append('</parameter>')
  605.             if dflt:
  606.                 sgml.append('<initializer>')
  607.                 sgml.append(dflt)
  608.                 sgml.append('</initializer>')
  609.             
  610.             sgml.append('</methodparam>\n')
  611.         
  612.         if not meth_def.params:
  613.             sgml.append('    <methodparam></methodparam>')
  614.         
  615.         sgml.append('  </methodsynopsis>')
  616.         return string.join(sgml, '')
  617.  
  618.     
  619.     def write_class_header(self, obj_name, fp):
  620.         if self.use_xml:
  621.             fp.write('<?xml version="1.0" standalone="no"?>\n')
  622.             fp.write('<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"\n')
  623.             fp.write('    "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">\n')
  624.         
  625.         fp.write('<refentry id="' + self.make_class_ref(obj_name) + '">\n')
  626.         fp.write('  <refmeta>\n')
  627.         fp.write('    <refentrytitle>%s</refentrytitle>\n' % self.pyname(obj_name))
  628.         fp.write('    <manvolnum>3</manvolnum>\n')
  629.         fp.write('    <refmiscinfo>PyGTK Docs</refmiscinfo>\n')
  630.         fp.write('  </refmeta>\n\n')
  631.         fp.write('  <refnamediv>\n')
  632.         fp.write('    <refname>%s</refname><refpurpose></refpurpose>\n' % self.pyname(obj_name))
  633.         fp.write('  </refnamediv>\n\n')
  634.  
  635.     
  636.     def write_class_footer(self, obj_name, fp):
  637.         fp.write('</refentry>\n')
  638.  
  639.     
  640.     def write_heading(self, text, fp):
  641.         fp.write('  <refsect1>\n')
  642.         fp.write('    <title>' + text + '</title>\n\n')
  643.  
  644.     
  645.     def close_section(self, fp):
  646.         fp.write('  </refsect1>\n')
  647.  
  648.     
  649.     def write_synopsis(self, obj_def, fp):
  650.         fp.write('<classsynopsis language="python">\n')
  651.         fp.write('  <ooclass><classname>%s</classname></ooclass>\n' % self.pyname(obj_def.c_name))
  652.         if isinstance(obj_def, definitions.ObjectDef):
  653.             if obj_def.parent:
  654.                 fp.write('  <ooclass><classname><link linkend="%s">%s</link></classname></ooclass>\n' % (self.make_class_ref(obj_def.parent), self.pyname(obj_def.parent)))
  655.             
  656.             for base in obj_def.implements:
  657.                 fp.write('  <ooclass><classname><link linkend="%s">%s</link></classname></ooclass>\n' % (self.make_class_ref(base), self.pyname(base)))
  658.             
  659.         elif isinstance(obj_def, definitions.InterfaceDef):
  660.             fp.write('  <ooclass><classname>gobject.GInterface</classname></ooclass>\n')
  661.         elif isinstance(obj_def, definitions.BoxedDef):
  662.             fp.write('  <ooclass><classname>gobject.GBoxed</classname></ooclass>\n')
  663.         elif isinstance(obj_def, definitions.PointerDef):
  664.             fp.write('  <ooclass><classname>gobject.GPointer</classname></ooclass>\n')
  665.         
  666.         constructor = self.parser.find_constructor(obj_def, self.overrides)
  667.         if constructor:
  668.             fp.write('%s\n' % self.create_constructor_prototype(constructor))
  669.         
  670.         methods = self.parser.find_methods(obj_def)
  671.         methods = filter((lambda meth, self = self: not self.overrides.is_ignored(meth.c_name)), methods)
  672.         for meth in methods:
  673.             fp.write('%s\n' % self.create_method_prototype(meth, addlink = 1))
  674.         
  675.         fp.write('</classsynopsis>\n\n')
  676.  
  677.     
  678.     def write_hierarchy(self, obj_name, ancestry, fp):
  679.         fp.write('<synopsis>')
  680.         indent = ''
  681.         for name, interfaces in ancestry:
  682.             fp.write(indent + '+-- <link linkend="' + self.make_class_ref(name) + '">' + self.pyname(name) + '</link>')
  683.             if interfaces:
  684.                 fp.write(' (implements ')
  685.                 for i in range(len(interfaces)):
  686.                     fp.write('<link linkend="%s">%s</link>' % (self.make_class_ref(interfaces[i]), self.pyname(interfaces[i])))
  687.                     if i != len(interfaces) - 1:
  688.                         fp.write(', ')
  689.                         continue
  690.                 
  691.                 fp.write(')\n')
  692.             else:
  693.                 fp.write('\n')
  694.             indent = indent + '  '
  695.         
  696.         fp.write('</synopsis>\n\n')
  697.  
  698.     
  699.     def write_params(self, params, ret, func_doc, fp):
  700.         if not params:
  701.             if not ret or ret == 'none':
  702.                 return None
  703.             
  704.         fp.write('  <variablelist>\n')
  705.         for type, name, dflt, null in params:
  706.             if func_doc:
  707.                 descr = string.strip(func_doc.get_param_description(name))
  708.             else:
  709.                 descr = 'a ' + type
  710.             fp.write('    <varlistentry>\n')
  711.             fp.write('      <term><parameter>%s</parameter> :</term>\n' % name)
  712.             fp.write('      <listitem><simpara>%s</simpara></listitem>\n' % self.reformat_text(descr, singleline = 1))
  713.             fp.write('    </varlistentry>\n')
  714.         
  715.         if ret and ret != 'none':
  716.             if func_doc and func_doc.ret:
  717.                 descr = string.strip(func_doc.ret)
  718.             else:
  719.                 descr = 'a ' + ret
  720.             fp.write('    <varlistentry>\n')
  721.             fp.write('      <term><emphasis>Returns</emphasis> :</term>\n')
  722.             fp.write('      <listitem><simpara>%s</simpara></listitem>\n' % self.reformat_text(descr, singleline = 1))
  723.             fp.write('    </varlistentry>\n')
  724.         
  725.         fp.write('  </variablelist>\n')
  726.  
  727.     
  728.     def write_constructor(self, func_def, func_doc, fp):
  729.         prototype = self.create_constructor_prototype(func_def)
  730.         fp.write('<programlisting>%s</programlisting>\n' % prototype)
  731.         self.write_params(func_def.params, func_def.ret, func_doc, fp)
  732.         if func_doc and func_doc.description:
  733.             fp.write(self.reformat_text(func_doc.description))
  734.         
  735.         fp.write('\n\n\n')
  736.  
  737.     
  738.     def write_method(self, meth_def, func_doc, fp):
  739.         fp.write('  <refsect2 id="' + self.make_method_ref(meth_def) + '">\n')
  740.         fp.write('    <title>' + self.pyname(meth_def.of_object) + '.' + meth_def.name + '</title>\n\n')
  741.         prototype = self.create_method_prototype(meth_def)
  742.         fp.write('<programlisting>%s</programlisting>\n' % prototype)
  743.         self.write_params(meth_def.params, meth_def.ret, func_doc, fp)
  744.         if func_doc and func_doc.description:
  745.             fp.write(self.reformat_text(func_doc.description))
  746.         
  747.         fp.write('  </refsect2>\n\n\n')
  748.  
  749.     
  750.     def output_toc(self, files, fp = sys.stdout):
  751.         if self.use_xml:
  752.             fp.write('<?xml version="1.0" standalone="no"?>\n')
  753.             fp.write('<!DOCTYPE reference PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"\n')
  754.             fp.write('    "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd">\n')
  755.             fp.write('<reference id="class-reference" xmlns:xi="http://www.w3.org/2001/XInclude">\n')
  756.             fp.write('  <title>Class Reference</title>\n')
  757.             for filename, obj_def in files:
  758.                 fp.write('  <xi:include href="%s"/>\n' % filename)
  759.             
  760.             fp.write('</reference>\n')
  761.         else:
  762.             fp.write('<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook V4.1.2//EN" [\n')
  763.             for filename, obj_def in files:
  764.                 fp.write('  <!ENTITY ' + string.translate(obj_def.c_name, self._DocbookDocWriter__transtable) + ' SYSTEM "' + filename + '" >\n')
  765.             
  766.             fp.write(']>\n\n')
  767.             fp.write('<book id="index">\n\n')
  768.             fp.write('  <bookinfo>\n')
  769.             fp.write('    <title>PyGTK Docs</title>\n')
  770.             fp.write('    <authorgroup>\n')
  771.             fp.write('      <author>\n')
  772.             fp.write('        <firstname>James</firstname>\n')
  773.             fp.write('        <surname>Henstridge</surname>\n')
  774.             fp.write('      </author>\n')
  775.             fp.write('    </authorgroup>\n')
  776.             fp.write('  </bookinfo>\n\n')
  777.             fp.write('  <chapter id="class-hierarchy">\n')
  778.             fp.write('    <title>Class Hierarchy</title>\n')
  779.             fp.write('    <para>Not done yet</para>\n')
  780.             fp.write('  </chapter>\n\n')
  781.             fp.write('  <reference id="class-reference">\n')
  782.             fp.write('    <title>Class Documentation</title>\n')
  783.             for filename, obj_def in files:
  784.                 fp.write('&' + string.translate(obj_def.c_name, self._DocbookDocWriter__transtable) + ';\n')
  785.             
  786.             fp.write('  </reference>\n')
  787.             fp.write('</book>\n')
  788.  
  789.  
  790. if __name__ == '__main__':
  791.     
  792.     try:
  793.         (opts, args) = getopt.getopt(sys.argv[1:], 'd:s:o:', [
  794.             'defs-file=',
  795.             'override=',
  796.             'source-dir=',
  797.             'output-prefix='])
  798.     except getopt.error:
  799.         e = None
  800.         sys.stderr.write('docgen.py: %s\n' % e)
  801.         sys.stderr.write('usage: docgen.py -d file.defs [-s /src/dir] [-o output-prefix]\n')
  802.         sys.exit(1)
  803.  
  804.     defs_file = None
  805.     overrides_file = None
  806.     source_dirs = []
  807.     output_prefix = 'docs'
  808.     for opt, arg in opts:
  809.         if opt in ('-d', '--defs-file'):
  810.             defs_file = arg
  811.         
  812.         if opt in ('--override',):
  813.             overrides_file = arg
  814.         elif opt in ('-s', '--source-dir'):
  815.             source_dirs.append(arg)
  816.         elif opt in ('-o', '--output-prefix'):
  817.             output_prefix = arg
  818.         
  819.     
  820.     if len(args) != 0 or not defs_file:
  821.         sys.stderr.write('usage: docgen.py -d file.defs [-s /src/dir] [-o output-prefix]\n')
  822.         sys.exit(1)
  823.     
  824.     d = DocbookDocWriter()
  825.     d.add_sourcedirs(source_dirs)
  826.     d.add_docs(defs_file, overrides_file, 'gtk')
  827.     d.output_docs(output_prefix)
  828.  
  829.